﻿package com.cw.war.ui
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Point;
	
	import net.libra.ui.base.BaseSprite;
	import net.libra.ui.layer.LayerDef;
	import net.libra.ui.layer.LayerManager;

	/**
	 * 弹出窗口基类。
	 * @author elvisjiang
	 */
	public class AbstractPopup extends BaseSprite
	{
		protected var container : Sprite;
		private var modalWin : Sprite;
		private var modal : Boolean;
		public var isPopUped:Boolean = false;
		private var _isMulitLayer:Boolean;
		public static var BOX_CLOSE:String = "box_close";
		
		public function AbstractPopup (isMulitLayer:Boolean = true)
		{
			_isMulitLayer = isMulitLayer;
			this.focusRect = null;
			this.tabEnabled = false;

			this.container = LayerManager.getLayer(LayerDef.BOX);
			this.modalWin = new Sprite ();
			this.modalWin.graphics.beginFill ( 0 , 0.2 );
			this.modalWin.graphics.drawRect ( 0 , 0 , 1 , 1 );
			this.modalWin.graphics.endFill ();
			//
			this.addEventListener ( Event.ADDED_TO_STAGE , this.__onAddToStage );
			this.addEventListener ( Event.REMOVED_FROM_STAGE , this.__onRemoveFromStage );
		}

		// ---------------------------------------------------------------------------------------------
		private function __onAddToStage ( e : Event = null ) : void
		{
			this.paintModalBg ();
			this.stage.addEventListener ( Event.RESIZE , this.__onResize );
		}

		private function __onRemoveFromStage ( e : Event = null ) : void
		{
			this.clearModelBg ();
			this.stage.removeEventListener ( Event.RESIZE , this.__onResize );
		}

		private function __onResize ( e : Event ) : void
		{
			if (this.modalWin.parent) this.container.removeChild ( this.modalWin );
			this.layout ();
			this.paintModalBg ();
		}

		private function paintModalBg () : void
		{
			if (this.modal)
			{
				this.modalWin.width = LayerManager.getStage().stageWidth * 4;
				this.modalWin.height = LayerManager.getStage().stageHeight * 4;
				// 计算modalWin的位置
				var pt : Point = this.parent.globalToLocal ( new Point () );
				this.modalWin.x = pt.x - this.modalWin.width / 2;
				this.modalWin.y = pt.y - this.modalWin.height / 2;
				if (! this.modalWin.parent) this.container.addChildAt ( this.modalWin , this.container.getChildIndex ( this ) );
			}
			else
			{
				if (this.modalWin.parent) this.container.removeChild ( this.modalWin );
			}
		}

		private function clearModelBg () : void
		{
			if (this.modalWin.parent) this.container.removeChild ( this.modalWin );
		}

		/**
		 * 是否是模式窗口
		 * @param b		是否是模式窗口
		 */
		public function setModal ( b : Boolean ) : void
		{
			this.modal = b;
			if (this.stage) this.paintModalBg ();
		}

		/**
		 * 返回是否是模式窗口
		 * @return		是否是模式窗口
		 */
		public function isModal () : Boolean
		{
			return this.modal;
		}

	
		/**
		 * 显示弹出窗口
		 */
		public function open () : void
		{
			if(!_isMulitLayer){
				BoxManager.closeAllBox();
			}
			this.layout ();
			this.container.addChild ( this );
			BoxManager.addBox(this);
			
			isPopUped = true;
		}

		private function layout () : void
		{
			// 布局
			var width : int;
			var height : int;
			if (this.container is Stage)
			{
				width = Stage ( this.container ).stageWidth;
				height = Stage ( this.container ).stageHeight;
			}
			else if (this.container.parent is Stage)
			{
				width = Stage ( this.container.parent ).stageWidth;
				height = Stage ( this.container.parent ).stageHeight;
			}
			else
			{
//				width = this.container.width;
//				height = this.container.height;
				width = LayerManager.getStage().stageWidth;
				height = LayerManager.getStage().stageHeight;
			}
			this.x = int ( (width - this.width) / 2 ) ;
			this.y = int ( (height - this.height) / 2 ) ;
		}
		/**
		 * 关闭弹出窗口。
		 * 方法会删除所有的子对象应用和侦听器。 这意味着调用此方法后，此窗口将彻底销毁不能再次使用
		 */
		public function close () : void
		{
			if (this.parent)
			{
				this.container.removeChild ( this );
			}
			dispatchEvent(new Event(BOX_CLOSE));
			BoxManager.removeBox(this);
			
			isPopUped = false;
		}
		
		override public function destroy () : void
		{
			this.container = null;
			super.destroy();
		}
		/**
		 * 返回弹出窗口的父容器
		 * @return		弹出窗口的父容器
		 */
		public function getContainer () : DisplayObjectContainer
		{
			return this.container;
		}

		/**
		 * 设置模式窗口背景的颜色值。
		 * @param alpha		背景颜色值
		 */
		public function setModalAlpha ( alpha : Number ) : void
		{
			this.modalWin.graphics.clear ();
			this.modalWin.graphics.beginFill ( 0 , alpha );
			this.modalWin.graphics.drawRect ( 0 , 0 , 1 , 1 );
			this.modalWin.graphics.endFill ();
		}

		// -----------------------------------------------------------------------------------------------------
		public function addOpenListener ( listener : Function , useCapture : Boolean = false , priority : int = 0 , useWeakReference : Boolean = false ) : void
		{
			this.addEventListener ( "AbstractPopup.open" , listener , useCapture , priority , useWeakReference );
		}

		public function removeOpenListener ( listener : Function , useCapture : Boolean = false ) : void
		{
			this.removeEventListener ( "AbstractPopup.open" , listener , useCapture );
		}

		public function addCloseListener ( listener : Function , useCapture : Boolean = false , priority : int = 0 , useWeakReference : Boolean = false ) : void
		{
			this.addEventListener ( "AbstractPopup.close" , listener , useCapture , priority , useWeakReference );
		}

		public function removeCloseListener ( listener : Function , useCapture : Boolean = false ) : void
		{
			this.removeEventListener ( "AbstractPopup.close" , listener , useCapture );
		}
	}
}
